package com.baidu.util;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;


/**
 * httpclient请求工具类
 * @author libs
 * 2016-1-14 10:55
 */
@SuppressWarnings("deprecation")
public class HttpClientUtil {
    
    private volatile static HttpClientUtil bc;
    
	private CookieStore cookieStore = new BasicCookieStore();
	
	private HttpClientUtil(){}
	
	public static HttpClientUtil getInstance(){
		TestUtil.systemOutPrintln("初始化：BaiduConnectService.");
		if(bc == null){
		    synchronized (HttpClientUtil.class) {  
                if (bc == null) {  
                    bc = new HttpClientUtil();  
                }  
		    }  
		}
		return bc;
	}
	
	/**
	 * 百度post/get请求
	 * @param url
	 * @param bduss
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "resource" })
	public HttpResponse execute(String url, String bduss, List<NameValuePair> params) throws Exception{
	    cookieStore = new BasicCookieStore();
		HttpClient httpClient = new DefaultHttpClient(
				new ThreadSafeClientConnManager());
		HttpResponse response = null;
		HttpUriRequest request = null;
		if (params != null) {
			HttpPost httpPost = new HttpPost(url);
			try {
				HttpEntity postBodyEnt = new UrlEncodedFormEntity(params, "utf-8");
				httpPost.setEntity(postBodyEnt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			request = httpPost;
		} else {
			HttpGet httpGet = new HttpGet(url);
			request = httpGet;
		}
		request.setHeader("Content-Type", "application/x-www-form-urlencoded");
		if(bduss != null){
			BasicClientCookie cookie = new BasicClientCookie("BDUSS", bduss);
			cookie.setVersion(0);
			cookie.setDomain(".baidu.com");
			cookie.setPath("/");
			cookieStore.addCookie(cookie);
		}
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
		HttpContext localContext = new BasicHttpContext();
		localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        response = httpClient.execute(request, localContext);
		TestUtil.systemOutPrintln("[HTTP状态码:" + response.getStatusLine().getStatusCode() + "]" + "-->Request URL:" + url);
		return response;
	}
	
	public CookieStore getCookieStore() {
		return cookieStore;
	}

	public void setCookieStore(CookieStore cookieStore) {
		this.cookieStore = cookieStore;
	}
}
